//
//  MineViewController.m
//  预购清单
//
//  Created by  on 2022/10/15.
//

#import "MineViewController.h"
#import "MineCell.h"
#import "RateViewController.h"
#import "ScoreViewController.h"
#import "BudgetViewController.h"
#import "LoginViewController.h"
#import "PrivateViewController.h"
@interface MineViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (strong, nonatomic) IBOutlet UIButton *avatar;

@property (strong, nonatomic) IBOutlet UILabel *name;
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSArray *imgs;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self initUI];
    [NotifyTool add:self :@selector(loadData) :notifyUserUpdate];
}
- (void)initUI
{
    self.title = LS(@"我的");
    self.backBtn.hidden = YES;
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.layer.masksToBounds = NO;
    self.tableView.layer.shadowColor = [UIColor colorHex:@"#9B9B9B"].CGColor;
    self.tableView.layer.shadowOffset = CGSizeMake(0, 3);
    self.tableView.layer.shadowRadius = 12;
    self.tableView.layer.cornerRadius = 12;
    self.tableView.layer.shadowOpacity = 1;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.scrollEnabled = NO;
    [self.tableView registerNib:[MineCell nib] forCellReuseIdentifier:[MineCell ID]];
    self.tableView.c_top.equalTo(self.name.c_bottom).coffset(35);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(15);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(-15);
    self.tableView.c_height.c_equal(self.array.count * 50);
}
- (void)loadData{
    if ([APP.user.ID isEqualToString:defaulUserID]) {
        self.array = @[@"汇率计算",@"预算中心",@"应用好评",@"隐私政策",@"去登录"];
        self.imgs = @[@"汇率计算",@"预算中心",@"应用好评",@"隐私政策",@"退出登录"];
        [self.avatar setImage:[UIImage imageNamed:@"默认头像"] forState:UIControlStateNormal];
    }else{
        self.array = @[@"汇率计算",@"预算中心",@"应用好评",@"隐私政策",@"退出登录",@"账号注销"];
        self.imgs = @[@"汇率计算",@"预算中心",@"应用好评",@"隐私政策",@"退出登录",@"账号注销"];
        [self.avatar setImage:[UIImage imageWithData:APP.user.headImg] forState:UIControlStateNormal];
    }
    self.name.text = LS(APP.user.username);
    self.tableView.c_height.c_equal(self.array.count * 50);
    [self.tableView reloadData];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if ([APP.user.account isEqualToString:defaulUserID]) {
        return [self gotoLogin];
    }
    WS(self)
    SheetViewController *sheetvc = [SheetViewController new];
    sheetvc.withCancel = NO;
    sheetvc.array = @[@"拍摄",@"相册"];
    sheetvc.block = ^(int index, NSString * _Nonnull text) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [CCHud startHud];
        });
        UIImagePickerController *vc = [UIImagePickerController new];
        vc.delegate = weakself;
        vc.allowsEditing = YES;
        vc.sourceType = index == 0 ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
        vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [[weakself currentVC] presentViewController:vc animated:YES completion:^{
            [CCHud stopHud];
        }];
    };
    [[weakself currentVC] presentViewController:sheetvc animated:NO completion:nil];
}
- (void)gotoLogin{
    LoginViewController * vc = [LoginViewController new];
    vc.islogin = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UIImagePickerControllerDelegate / UINavigationControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    UIImage *img = info[UIImagePickerControllerEditedImage];
    APP.user.headImg = UIImageJPEGRepresentation(img, 0.5);
    [UserTool update:APP.user complete:^(NSDictionary * _Nonnull dict) {
        
    }];
    [self loadData];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:[MineCell ID]];
    cell.name.text = LS(self.array[indexPath.row]);
    cell.img.image = [UIImage imageNamed:self.imgs[indexPath.row]];
    cell.line.hidden = indexPath.row == [self.array count] - 1;
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * str = self.array[indexPath.row];
    if ([str isEqualToString:@"汇率计算"]) {
        RateViewController * vc= [RateViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([str isEqualToString:@"预算中心"]){
        BudgetViewController * vc= [BudgetViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if([str isEqualToString:@"应用好评"]){
        ScoreViewController * vc= [ScoreViewController new];
        [self presentViewController:vc animated:NO completion:nil];
    }else if ([str isEqualToString:@"隐私政策"]){
        PrivateViewController * vc= [PrivateViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([str isEqualToString:@"退出登录"]){
        APP.user = [User new];
    }else if ([str isEqualToString:@"去登录"]){
        [self gotoLogin];
    }else if ([str isEqualToString:@"账号注销"]){
        [self alert:LS(@"注销账号") :LS(@"您确定要注销账号吗？") :^{
        } :^{
            [UserTool delete:APP.user complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = [User new];
                }
            }];
        }];
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == self.array.count - 1) {
        UIBezierPath *path;
        if (indexPath.row == 0) {
            path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
        }else{
            path = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
        }
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.frame = cell.bounds;
        layer.path = path.CGPath;
        cell.layer.mask = layer;
    }else{
        cell.layer.mask = nil;
    }
}
@end

